import { HTTP } from '../util/http-p'

class KeywordModel extends HTTP {
  key = 'q'
  maxLength = 10
  getHistory() {
    return wx.getStorageSync(this.key)
  }

  getHot() {
    return this.request({
      url: 'book/hot_keyword',
    })
  }

  addToHistory(keyword) {
    let words = this.getHistory() || []
    if (!words.includes(keyword)) {
      if (words.length >= this.maxLength) {
        words.pop()
      }
      words.unshift(keyword)
      wx.setStorageSync(this.key, words)
    }
  }

  search(start, q) {
    return this.request({
      url: 'book/search?summary=1',
      data: {
        q: q,
        start: start,
      },
    })
  }
}

export { KeywordModel }
